﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS.DAL.Providers
{
    public class StockProvider : ProviderBase<Stock, StockSearchCriteria>
    {
        public override void Insert(Stock item)
        {
            Context.Stocks.AddObject(item);
            Context.SaveChanges();
        }

        public override void Update(Stock updatedItem)
        {
            var StockFound = (from s in Context.Stocks
                              where s.ID == updatedItem.ID
                              select s).SingleOrDefault();
            StockFound.Address = updatedItem.Address;
            StockFound.Mobile = updatedItem.Mobile;
            StockFound.Type = updatedItem.Type;
            StockFound.Name = updatedItem.Name;
            Context.SaveChanges();
        }

        public override void Delete(int id)
        {
            Context.Stocks.DeleteObject(this.GetByID(id));
            Context.SaveChanges();
        }

        public override Stock GetByID(int id)
        {
            return (from s in Context.Stocks
                    where (s.ID == id)
                    select s).SingleOrDefault();
        }

        public override List<Stock> Search(StockSearchCriteria SearchCriteria)
        {
            return (from s in Context.Stocks
                    where ((SearchCriteria.Name == String.Empty) || (s.Name.Contains(SearchCriteria.Name))) &&
                     ((SearchCriteria.type == null) || (s.Type == (int?)(SearchCriteria.type)))
                    select s).ToList();

        }


    }
}
